﻿CREATE PROCEDURE [acms].[Site_Put]
(
	@Id int,
	@Name varchar(50),
	@RootUrl varchar(255),
	@CID int,
	@IdOut int output,
	@CreatedOut datetime output,
	@CIDOut int output
)	
AS
	SET NOCOUNT ON
	if exists (select null from [Site] where Id = @Id and deleted = 0) begin
		update [Site]
		set
			Name = @Name,
			RootUrl = @RootUrl,
			CID = @CID + 1
		where Id = @Id
			and CID = @CID
		
		if @@rowcount = 0
			return -100
			
		set @IdOut = @Id
	end
	else begin
		insert [Site]
			(Name, RootUrl)
		values
			(@Name, @RootUrl)
		set @IdOut = @@identity
	end
	select @CreatedOut = [CreatedDateTime], @CIDOut = [CID] from [Site] where Id = @IdOut
	RETURN


